local chidan = fk.CreateSkill({
  name = "lingling__chidan",
})

Fk:loadTranslationTable{
  ["lingling__chidan"] = "赤胆",
  [":lingling__chidan"] = "当你受到伤害后，你可以选择：收回装备区所有非防具牌，然后使用至多等量张牌；移动伤害来源装备区一张牌至你对应装备栏。",

  ["lingling__chidan_equip"] = "收回非防具装备，然后可以使用牌",
  ["lingling__chidan_move"] = "将%dest一张装备移动给你",
  ["#lingling__chidan-use"] = "赤胆：你可以使用一张牌（第%arg张，共%arg2张）",
}

chidan:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(chidan.name) then
      if table.find(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).sub_type ~= Card.SubtypeArmor
      end) then
        return true
      else
        return data.from and not data.from.dead and data.from ~= player and
          table.find(data.from:getCardIds("e"), function (id)
            return data.from:canMoveCardInBoardTo(player, id)
          end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    if table.find(player:getCardIds("e"), function (id)
      return Fk:getCardById(id).sub_type ~= Card.SubtypeArmor
    end) then
      table.insert(choices, "lingling__chidan_equip")
    end
    if data.from and not data.from.dead and data.from ~= player and
      room:canMoveCardInBoard("e", {data.from}, {}, {player}) then
      table.insert(choices, "lingling__chidan_move::"..data.from.id)
    end
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = chidan.name,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice == "lingling__chidan_equip" then
      local cards = table.filter(player:getCardIds("e"), function (id)
        return Fk:getCardById(id).sub_type ~= Card.SubtypeArmor
      end)
      local n = #cards
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, chidan.name, nil, true, player)
      if player.dead then return end
      for i = 1, n do
        local use = room:askToPlayCard(player, {
          skill_name = chidan.name,
          prompt = "#lingling__chidan-use:::"..i..":"..n,
          cancelable = true,
          extra_data = {
            bypass_times = true,
            extraUse = true,
          },
          skip = true,
        })
        if use then
          use.extraUse = true
          room:useCard(use)
        else
          return
        end
      end
    else
      room:askToMoveCardInBoard(player, {
        target_one = data.from,
        target_two = player,
        skill_name = chidan.name,
        flag = "e",
        move_from = data.from,
      })
    end
  end,
})

return chidan
